﻿using UnityEngine;
using System.Collections;

public class PickUpItem : MonoBehaviour {
    //所有的格子
    public GameObject[] cells;
    //随机类型种类
    private string[] equipmentType = { "Wuqi","Kuijia","Shouzhuo","Shuijing","Lihe" };
    //创建的新物品载体
    public GameObject item;
    //检查物品栏满溢程度，上限30；
    private int num = 0;
    //警告窗口
    public GameObject warming;

    void Start()
    {
        warming.SetActive(false);
    }

	void Update () {
        if (Input.GetKeyDown(KeyCode.W))
        {
            PickUp();
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            DelItem();
        }
	}

    public void PickUp()
    {
        Item newItem = RandomItem(); 
        //该标志位判断是否成功添加了
        bool isFind = false;
        //先判断格子里是否有该种类的物品，是否可以叠加
        for (int i = 0; i < cells.Length; i++)
        {
            if (cells[i].transform.childCount > 0)
            {
                DragItem TargetItem = cells[i].GetComponentInChildren<DragItem>();
                if (TargetItem.ThisItem.Name == newItem.Name)
                {
                    if (Item.IsOneOf(newItem.ItemType) > 0)
                    {
                        TargetItem.AddNum(1);
                        isFind = true;
                        break;
                    }
                    else
                    {                       
                        isFind = false;
                    }
                }
            }
        }
        //没有相同物品且不可叠加，找空格子放
        if (isFind == false)
        {
            num = CellChildNum();
            if (num >= 30)
            {
                //如果格子已满显示警告窗口
                warming.SetActive(true);
            }
            else
            {
                for (int i = 0; i < cells.Length; i++)
                {
                    if (cells[i].transform.childCount == 0)
                    {
                        GameObject obj = NGUITools.AddChild(cells[i], item);
                        obj.GetComponent<DragItem>().ThisItem = newItem;
                        obj.GetComponent<UISprite>().spriteName = newItem.Name;
                        obj.transform.localPosition = Vector3.zero;
                        if (Item.IsOneOf(newItem.ItemType) < 1)
                        {
                            obj.GetComponentInChildren<UILabel>().transform.gameObject.SetActive(false);
                        }                        
                        break;
                    }
                }
            }          
        }
    }
    
    //随机创建一个物品，模拟捡到东西
    public Item RandomItem()
    {
        Item newItem = new Item();

        int index = Random.Range(0, equipmentType.Length);

        newItem.ItemType = equipmentType[index];

        switch (index)
        {
            case 0:
                newItem.Name = "Wuqi";
                newItem.Att = 832;
                break;
            case 1:
                newItem.Name = "Kuijia";
                newItem.Def = 520;
                newItem.Hp = 1500;
                break;
            case 2:
                newItem.Name = "Shouzuo";
                newItem.Hp = 320;
                newItem.Def = 200;
                break;
            case 3:
                newItem.Name = "Shuijing";
                newItem.Hp = 180;
                newItem.Sp = 780;
                break;
            case 4:
                newItem.Name = "Lihe";
                //消耗品就无属性的加成了
                break;
            default:
                Debug.Log("一般来说不会打印这里吧");
                break;
        }

        return newItem;
    }
    //删除最后一个格子里的物品
    public void DelItem()
    {
        int num = CellChildNum();
        if (num > 0)
        {
            Transform chil = cells[num - 1].transform.GetChild(0);
            Destroy(chil.gameObject);
        }
        
    }

    //计算格子满溢程度，返回存有物品的格子个数
    public int CellChildNum()
    {
        int num = 0;
        for (int i = 0; i < cells.Length; i++)
        {
            if (cells[i].transform.childCount > 0)
            {
                num++;
            }
        }
        return num;
    }

}
